package com.zmn.plat.admin.controller.channel.brandratio;

import java.util.List;
import java.util.Optional;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.model.entity.channel.ChannelCityPriceRatio;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.common.dictionary.BaseBrandRatioTypeEnum;
import com.zmn.plat.common.dictionary.ChannelBrandRatioTypeEnum;
import com.zmn.plat.common.dictionary.PriceTypeEnum;
import com.zmn.plat.model.entity.brand.BaseBrand;
import com.zmn.plat.model.entity.brand.BaseBrandQuery;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.model.entity.channel.brand.ratio.ChannelBrandRatio;
import com.zmn.plat.model.entity.channel.brand.ratio.ChannelBrandRatioQuery;
import com.zmn.plat.model.vo.channel.brand.ratio.ChannelBrandRatioVo;

/**
 * 类描述:渠道品牌价格系数管理查询控制
 *
 * @author linfeng
 * @since 2020/09/02/11:02
 */
@Controller
@RequestMapping("/channel/brand/ratio/")
public class ChannelBrandRatioListController extends ChannelBrandRatioBaseController {
    private Logger logger = LoggerFactory.getLogger(ChannelBrandRatioListController.class);

    /**
     * 描述: 渠道品牌系数列表
     *
     * @param query
     *            查询实体
     * @return ModelAndView
     */
    @RequestMapping(value = "list")
    public ModelAndView list(@ModelAttribute ChannelBrandRatioQuery query) {
        if (query.getPriceType() == null) {
            query.setPriceType(PriceTypeEnum.PRICE.getCode());
        }
        query.setRatioType(ChannelBrandRatioTypeEnum.BRANDRATIO.getCode());
        ModelAndView mav = new ModelAndView("channel/brand/ratio/list");
        try {
            if (query.getBizType() == ProductConsts.BIZ_TYPE_C) {
                // C类业务价格系数返回计价器3.0默认系数
                query.setMeterQuery(Boolean.TRUE);
            }
            List<ChannelBrandRatioVo> items = brandRatioService.listPageByQuery(query);
            Channel channel = channelService.findByKey(query.getChannelId());
            mav.addObject("vo", query);
            mav.addObject("channel", channel);
            mav.addObject("items", items);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return mav;
    }

    /**
     * 渠道品牌分类系数(一二级分类系数对应表)
     *
     * @param channelId
     * @param brandId
     * @param priceType
     * @param bizType
     * @param servCategId
     * @return
     */
    @RequestMapping(value = "categoryRatio")
    public ModelAndView categoryInRatio(@RequestParam(value = "channelId", defaultValue = "0") int channelId,
        @RequestParam(value = "brandId", defaultValue = "0") int brandId,
        @RequestParam(value = "priceType", defaultValue = "1") int priceType,
        @RequestParam(value = "bizType", defaultValue = "1") int bizType,
        @RequestParam(value = "servCategId", defaultValue = "0") int servCategId) {
        return categoryRatio(channelId, brandId, priceType, servCategId, bizType);
    }

    /**
     * 保内系数-设置分类系数
     *
     * @param brandId
     * @param servCategId
     * @param categOneId
     * @param categOneName
     * @param servCategName
     * @return
     */
    @RequestMapping(value = "categDetail")
    public ModelAndView categDetail(@RequestParam(value = "channelId", defaultValue = "0") int channelId,
        @RequestParam(value = "brandId", defaultValue = "0") int brandId,
        @RequestParam(value = "priceType", defaultValue = "1") int priceType,
        @RequestParam(value = "bizType", defaultValue = "1") int bizType,
        @RequestParam(value = "servCategId", defaultValue = "0") int servCategId,
        @RequestParam(value = "categOneId", defaultValue = "0") int categOneId,
        @RequestParam(value = "categOneName", defaultValue = "0") String categOneName,
        @RequestParam(value = "servCategName", defaultValue = "") String servCategName) {

        return detail(channelId, brandId, priceType, servCategId, categOneId, categOneName, bizType, servCategName);
    }

    /**
     * 渠道品牌默认系数
     *
     * @param brandId
     * @return
     */
    @RequestMapping("brandRatio")
    public ModelAndView brandRatio(@RequestParam(value = "channelId", defaultValue = "0") int channelId,
        @RequestParam(value = "brandId", defaultValue = "0") int brandId,
        @RequestParam(value = "priceType", defaultValue = "1") int priceType,
        @RequestParam(value = "bizType", defaultValue = "0") int bizType) {
        ChannelBrandRatioQuery brandRatioQuery = new ChannelBrandRatioQuery();
        brandRatioQuery.setBrandId(brandId);
        brandRatioQuery.setRatioType(BaseBrandRatioTypeEnum.BRANDRATIO.getCode());
        brandRatioQuery.setBizType(bizType);
        brandRatioQuery.setPriceType(priceType);
        brandRatioQuery.setChannelId(channelId);
        List<ChannelBrandRatio> brandRatios = brandRatioService.listByQuery(brandRatioQuery);
        ChannelBrandRatio brandRatio = new ChannelBrandRatio();
        if (brandRatios != null && brandRatios.size() > 0) {
            brandRatio = brandRatios.get(0);
        }

        if (NumberUtil.isNotNullOrZero(brandId) && NumberUtil.isNotNullOrZero(priceType)) {
            brandRatio.setPriceType(priceType);
        }

        BaseBrandQuery baseBrandQuery = new BaseBrandQuery();
        baseBrandQuery.setStatus(GlobalConsts.YES);
        List<BaseBrand> baseBrandList = baseBrandService.listByQuery(baseBrandQuery);
        ModelAndView mav = new ModelAndView("channel/brand/ratio/brandratio");
        if (bizType == ProductConsts.BIZ_TYPE_C) {
            // C类业务价格系数返回计价器3.0默认系数
            brandRatioQuery.setBizType(null);
            brandRatioQuery.setRatioType(BaseBrandRatioTypeEnum.BRANDMETER3RATIO.getCode());
            List<ChannelBrandRatio> brandRatioList =brandRatioService.listByQuery(brandRatioQuery);
            if(CollectionUtil.isNotNullOrEmpty(brandRatioList)){
                mav.addObject("meterRatio", brandRatioList.get(0).getToBeEffectiveRatio() == null ? brandRatioList.get(0).getRatio() : brandRatioList.get(0).getToBeEffectiveRatio());
            }
        }
        mav.addObject("brandRatio", brandRatio);
        mav.addObject("channelId", channelId);
        mav.addObject("brandId", brandId);
        mav.addObject("bizType", bizType);
        mav.addObject("priceType", priceType);
        mav.addObject("baseBrandList", baseBrandList);
        return mav;
    }
}
